package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.menu.Menu;

/**
 * @author: tlgen
 * @Discription:
 */
public class MenuServlet extends BaseServlet
{

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);//使a标签发送post请求
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();
        String methodName = servletPath.substring(1);
        methodName = methodName.substring(0,methodName.length()-3);

        try {
            //反射获取methName
            Method method=getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
            //利用反射调用方法
            method.invoke(this,request, response);

        }catch(Exception e) {

            response.sendRedirect("error.jsp");
        }
    }

    //显示所有权限
    private void list(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");


        List<Menu> menuList = new ArrayList<Menu>(); // 创建一个list集合,用于装user集合
        Connection connection = DbUtil.getConn(); // 打开数据库连接
        String sql = "select * from t_menu";

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet rs = preparedStatement.executeQuery();

            while (rs.next()) {
                String menuid = rs.getString("menuid");
                String menuName = rs.getString("menuName");
                String menupid = rs.getString("menupid");
                String url = rs.getString("url");
                Menu menu = new Menu();
                menu.setMenuid(menuid);
                menu.setMenuName(menuName);
                menu.setMenupid(menupid);
                menu.setUrl(url);
                menuList.add(menu);
            }
            request.setAttribute("menuList", menuList);
            request.getRequestDispatcher("/page/menu/menu_list.jsp").forward(request, response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    
  

    //更新权限
    private void update(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        String menuid = request.getParameter("menuid");
        String menupid = request.getParameter("menupid");
        String menuName = request.getParameter("menuName");
        String url = request.getParameter("url");

        if (menuid != null && menuName != null) {
            Connection conn = DbUtil.getConn();
            String sql = "update t_menu set menupid=?, menuName=?, url=? where menuid=" + menuid;

            try {
                PreparedStatement prtmt = conn.prepareStatement(sql);

                /*更新操作id值不变，不需要重新占位和重新设置值 更新不包括id 入坑*/
               
                prtmt.setString(1, menupid);
                prtmt.setString(2, menuName);
                prtmt.setString(3, url);

                int resInt = prtmt.executeUpdate();
                request.setAttribute("resInt", resInt);
                request.getRequestDispatcher("/list.mo").forward(request, response);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

    //添加权限
    private void add(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        String menuid = request.getParameter("menuid");
        String menuName = request.getParameter("menuName");
        String menupid = request.getParameter("menupid");
        String url = request.getParameter("url");

        Connection conn = DbUtil.getConn();

        /*这个null值是id对应数据库表中中的位置，才能正确作为占位符*/
        String sql = "insert into t_menu value(?,?,?,?);";

        try {
            PreparedStatement prtmt = conn.prepareStatement(sql);
            prtmt.setString(1, menuid);
            prtmt.setString(2, menuName);
            prtmt.setString(3, menupid);
            prtmt.setString(4, url);

            int resInt = prtmt.executeUpdate();
            request.setAttribute("resInt", resInt);

            request.getRequestDispatcher("/list.mo").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //修改权限
    private void edit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        String menuid = request.getParameter("menuid");
        Connection conn = DbUtil.getConn();
        String sql = "select * from t_menu where menuid = " + menuid;
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ResultSet rs = preparedStatement.executeQuery();

        try {
            if (rs.next()) {

                Menu menu = new Menu();
                menu.setMenuid(rs.getString("menuid"));
                menu.setMenuName(rs.getString("menuName"));
                menu.setMenupid(rs.getString("menupid"));
                menu.setUrl(rs.getString("url"));


                request.setAttribute("menu", menu);
                request.getRequestDispatcher("/page/menu/menu_edit.jsp").forward(request, response);

            }
        }catch (SQLException e) {
            e.printStackTrace();
        }
    }


    //删除权限
    private void delete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String menuid=request.getParameter("menuid");
        if (menuid != null) {
            Connection conn = DbUtil.getConn();
            String sql = "delete from t_menu where menuid = ? ";//不是得到id去编辑 不写成+id 这里的id可以批量去理解
            try {
                PreparedStatement prtmt = conn.prepareStatement(sql);

                prtmt.setString(1, menuid);
                int resInt = prtmt.executeUpdate();
                request.setAttribute("resInt", resInt);
                request.getRequestDispatcher("/list.mo").forward(request, response);

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
